package com.hyper_yang.javase.maps;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

public class Map_traverse {
    /*
        map集合遍历方式: 1. 键找值  keySet()
                      2. 键值对对象 entrySet()
                      3. lambda BiConsumer<K,V>
     */

    public static void main(String[] args) {
        Map<String,String> map = new HashMap<>();

        map.put("尹志平", "小龙女");
        map.put("陈平安", "宁姚");
        map.put("齐静春", "蔡金简");

        // 1. 键找值 keySet()
        // 将键放入单列集合,同单列集合来取值。
//        Set<String> keys = map.keySet();
//        Iterator<String> iterator = keys.iterator();
//        while (iterator.hasNext()) {
//            String key = iterator.next();
//            System.out.println(key + "==" + map.get(key));
//        }
//        for (String key : keys) {
//            System.out.println(key + "==" + map.get(key));
//        }
//        keys.forEach(key -> System.out.println(key + "==" + map.get(key)));

        // 2. 键值对 entrySet()
        // Entry是 Map的内部接口
//        Set<Map.Entry<String, String>> entries = map.entrySet();
//        for (Map.Entry<String, String> entry : entries) {
//            String key = entry.getKey();
//            String value = entry.getValue();
//            System.out.println(key + "==" + value);
//        }
//        Iterator<Map.Entry<String, String>> iterator = entries.iterator();
//        while (iterator.hasNext()) {
//            Map.Entry<String, String> entry = iterator.next();
//            String key = entry.getKey();
//            String value = entry.getValue();
//            System.out.println(key + "==" + value);
//        }
//        entries.forEach(new Consumer<Map.Entry<String, String>>() {
//            @Override
//            public void accept(Map.Entry<String, String> entry) {
//                String key = entry.getKey();
//                String value = entry.getValue();
//                System.out.println(key + "==" + value);
//            }
//        });
//        entries.forEach(entry -> {
//            String key = entry.getKey();
//            String value = entry.getValue();
//            System.out.println(key + "==" + value);
//        });

        // 3. lambda 表达式
//        map.forEach(new BiConsumer<String, String>() {
//            @Override
//            public void accept(String key, String value) {
//                System.out.println(key + "==" + value);
//            }
//        });
        map.forEach((key,value) -> System.out.println(key + "==" + value));
        /*  源码:
            default void forEach(BiConsumer<? super K, ? super V> action) {
                Objects.requireNonNull(action);
                for (Map.Entry<K, V> entry : entrySet()) {
                    K k;
                    V v;
                    try {
                        k = entry.getKey();
                        v = entry.getValue();
                    } catch (IllegalStateException ise) {
                        // this usually means the entry is no longer in the map.
                        throw new ConcurrentModificationException(ise);
                    }
                    action.accept(k, v);
                }
            }
         */
    }
}
